proto_header = '''syntax = "proto3";

package zpilot.candata;
'''

def gen(dp, logging):
    for node in dp.config.can_node_list:
        content = proto_header
        for message in dp.data:
            if message.can_node == node['node_name']:
                id = 1
                content += '''message {}_MSG
{{\n'''.format(message.name)
                for signal in message.signals:
                    if signal.min.startswith('-'):
                        # print(f"special proto: {signal.name}")
                        if signal.signal_type in ['uint8', 'uint16', 'uint32']:
                            t_signal_type = 'sint32'
                        elif signal.signal_type in ['sint8', 'sint16', 'sint32']:
                            t_signal_type = 'sint32'
                        else:
                            t_signal_type = signal.signal_type
                    else:
                        if signal.signal_type in ['uint8', 'uint16', 'uint32']:
                            t_signal_type = 'uint32'
                        elif signal.signal_type in ['sint8', 'sint16', 'sint32']:
                            t_signal_type = 'sint32'
                        else:
                            t_signal_type = signal.signal_type
                    content += '  {} {} = {};\n'.format(t_signal_type, signal.name, id)
                    id += 1
                content += '}\n\n'
#         content += '''message {}_MSG
# {{\n'''.format(node['node_name'])
#         id = 1
#         for message in dp.data:
#             if message.can_node == node['node_name']:
#                 content += '  {}_MSG {} = {};\n'.format(message.name,message.name, id)
#                 id += 1
#         content += '}\n\n'
        with open("output/{}.proto".format(node['node_name']),'w') as f:
                f.write(content)

